//
//  StringUtil.m
//  SZYH2
//
//  Created by ZZB on 13-7-18.
//  Copyright (c) 2013年 JMU. All rights reserved.
//

#import <CommonCrypto/CommonDigest.h>
#import "StringUtil.h"

@implementation StringUtil

+ (BOOL)isEmpty:(NSString *)string
{
    if (string == nil || [string isEqual:[NSNull null]]
        || [string isKindOfClass:[NSString class]] == NO || string.length <= 0) {
        return YES;
    }
    
    return NO;
}

+ (BOOL)isNotEmpty:(NSString *)string
{
    return ![StringUtil isEmpty:string];
}

+ (BOOL)isDateNum:(double)num
{
    if (num <= 0) {
        return NO;
    }
    
    return YES;
}

+ (BOOL)isNotDateNum:(double)num
{
    return ![StringUtil isDateNum:num];
}

/**
 * 格式化数字为字符串
 */
+ (NSString *)valueOfInt:(int)value
{
    return [NSString stringWithFormat:@"%d", value];
}

+ (NSString *)valueOfFloat:(float)value
{
    return [StringUtil valueOfFloat:value withPoint:10];
}

+ (NSString *)valueOfFloat:(float)value withPoint:(int)pointNum
{
    return [NSString stringWithFormat:@"%.*f", pointNum, value];
}

+ (NSString *)valueOfDouble:(double)value
{
    return [StringUtil valueOfDouble:value withPoint:10];
}

+ (NSString *)valueOfDouble:(double)value withPoint:(int)pointNum
{
    return [NSString stringWithFormat:@"%.*lf", pointNum, value];
}

+ (int)indexOf:(NSString*)strKey inString:(NSString*)string
{
    NSRange range = [string rangeOfString:strKey];
    if (range.location == NSNotFound) {
        return -1;
    }

    return (int)range.location;
}

+ (BOOL)containChars:(NSCharacterSet *)charset inString:(NSString *)string
{
    if ([StringUtil isEmpty:string] || charset == nil) {
        return NO;
    }
    
    NSRange range = [string rangeOfCharacterFromSet:charset];
    if (range.location == NSNotFound) {
        return NO;
    }
    
    return YES;
}

+ (BOOL)containString:(NSString *)key inString:(NSString *)string ignoreCase:(BOOL)ignoreCase {
    if ([StringUtil isEmpty:string] || [StringUtil isEmpty:key]) {
        return NO;
    }
    
    if (ignoreCase) {
        if ([string.lowercaseString rangeOfString:key.lowercaseString].location != NSNotFound) {
            return YES;
        }
    } else {
        if ([string rangeOfString:key].location != NSNotFound) {
            return YES;
        }
    }

    return NO;
}

+ (NSString *)trimHeadTail:(NSCharacterSet *)charset forString:(NSString *)string
{
    if ([StringUtil isEmpty:string]) {
        return string;
    }
    
    NSString *retString = [string stringByTrimmingCharactersInSet:charset];
    if ([StringUtil isEmpty:retString]) {
        return @"";
    }
    
    return retString;
}

+ (NSString *)md5:(NSString *)string
{
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5([string cStringUsingEncoding:NSUTF8StringEncoding], (unsigned int)[string length], result);
    
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]
            ];
}

#pragma mark - Private

+ (NSString *)doubleToString:(double)val
{
	@try {
		NSString *ret = [NSString stringWithFormat:@"%.0f", val];
		unichar c = [ret characterAtIndex:[ret length] - 1];
        
		while (c == 48 || c == 46) { // 0 or .
			ret = [ret substringToIndex:[ret length] - 1];
			c = [ret characterAtIndex:[ret length] - 1];
		}
        
		return ret;
	}
	@catch(NSException *e) {
		NSLog(@"doubleToString Catch Error:%@", e.reason);
	}
	@finally {
	}
    
	return @"";
}

+ (NSString *)domainFromEmail:(NSString *)email
{
    if (email == nil) {
        return @"";
    }

    //获取@前缀：
    NSRange range = [email rangeOfString:@"@"];
    if (range.location != NSNotFound) {
        if (range.location+1 < email.length) {
            return [email substringFromIndex:range.location+1];
        }
    }

    return @"";
}

+ (NSString *)stringFormat334:(NSString *)mobile
{
    NSString *fomat;
    if (3 >= mobile.length) {
        return mobile;
    }
    if (4 <= mobile.length && 7 >= mobile.length) {
        fomat = [NSString stringWithFormat:@"(\\d{3})(\\d{%lu})", mobile.length-3];
        return [mobile stringByReplacingOccurrencesOfString:fomat
                                                 withString:@"$1 $2"
                                                    options:NSRegularExpressionSearch
                                                      range:NSMakeRange(0, [mobile length])];
    }
    fomat = [NSString stringWithFormat:@"(\\d{3})(\\d{4})(\\d{%lu})", mobile.length-7];
    return [mobile stringByReplacingOccurrencesOfString:fomat
                                                               withString:@"$1 $2 $3"
                                                                  options:NSRegularExpressionSearch
                                                                    range:NSMakeRange(0, [mobile length])];
}

+ (NSString *)stringFormat44:(NSString *)mobile
{
    NSUInteger len = mobile.length;
    if (4 >= len) {
        return mobile;
    }
    NSString *fomat = [NSString stringWithFormat:@"(\\d{4})(\\d{%lu})", mobile.length-4];
    return [mobile stringByReplacingOccurrencesOfString:fomat
                                             withString:@"$1 $2"
                                                options:NSRegularExpressionSearch
                                                  range:NSMakeRange(0, [mobile length])];
}

@end
